package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.Search.KeywordBean;
import com.cskaoyan.bean.Search.SearchIndexVo;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.mapper.MarketSearchHistoryMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    MarketKeywordMapper marketKeywordMapper;

    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;

    @Override
    public SearchIndexVo showIndex() {

        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        SearchIndexVo searchIndexVo = new SearchIndexVo();


        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        List<MarketSearchHistory> marketSearchHistories = null;

        if (principals != null) {
            MarketUser primaryPrincipal = (MarketUser) principals.getPrimaryPrincipal();

            Integer uid = primaryPrincipal.getId();
            MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
            MarketSearchHistoryExample.Criteria criteria = marketSearchHistoryExample.createCriteria();
            criteria.andUserIdEqualTo(uid);
            criteria.andDeletedEqualTo(false);
            marketSearchHistoryExample.setOrderByClause("update_time desc");


            //历史关键字
            marketSearchHistories = marketSearchHistoryMapper.selectByExample(marketSearchHistoryExample);
            //历史关键字赋值
            List<MarketSearchHistory> objects = new ArrayList<>();
            for (MarketSearchHistory marketSearchHistory : marketSearchHistories) {
                MarketSearchHistory marketSearchHistory1 = new MarketSearchHistory();
                marketSearchHistory1.setKeyword(marketSearchHistory.getKeyword());
                marketSearchHistory1.setDeleted(marketSearchHistory.getDeleted());
                objects.add(marketSearchHistory1);
            }
            searchIndexVo.setHistoryKeywordList(objects);
        }


        //默认的关键字
        MarketKeyword marketKeyword = marketKeywordMapper.selectByPrimaryKey(6);

        //火热的关键字
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(marketKeywordExample);


        //默认的关键字赋值
        searchIndexVo.setDefaultKeyword(marketKeyword);

        //火热的关键字赋值
        searchIndexVo.setHotKeywordList(marketKeywords);

        //返回数据
        return searchIndexVo;
    }

    @Override
    public void insertKeyword(String keyword) {
        //先判断这个关键词在不在表格里面，有的话就不插入，退出
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        marketSearchHistoryExample.createCriteria().andKeywordEqualTo(keyword);
        List<MarketSearchHistory> searchHistories = marketSearchHistoryMapper.selectByExample(marketSearchHistoryExample);
        if (searchHistories.size()!=0) {
            return;
        }

        //到这里就是没有这个关键词，直接插入
        //赋值
        MarketSearchHistory marketSearchHistory = new MarketSearchHistory();
        marketSearchHistory.setKeyword(keyword);
        marketSearchHistory.setDeleted(false);
        marketSearchHistory.setFrom("wx");

        //UID
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer uid = primaryPrincipal.getId();
        marketSearchHistory.setUserId(uid);

        marketSearchHistoryMapper.insertSelective(marketSearchHistory);
    }

    @Override
    public void cleanHistory(Integer uid) {
//        MarketSearchHistory marketSearchHistory = new MarketSearchHistory();
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();

        marketSearchHistoryExample.createCriteria().andUserIdEqualTo(uid);
        marketSearchHistoryMapper.deleteByExample(marketSearchHistoryExample);
    }


}
